import Vue from 'vue'

const TimeStr = (time) => {
  const newDate = new Date(time);
  const YY = newDate.getFullYear();
  const MM = newDate.getMonth() + 1;
  const DD = newDate.getDate();
  const hh = newDate.getHours();
  const mm = newDate.getMinutes();
  const ss = newDate.getSeconds();

  return `${YY}-${MM < 10 ? "0" + MM : MM}-${DD < 10 ? "0" + DD : DD} ${hh < 10 ? "0" + hh : hh
    }:${mm < 10 ? "0" + mm : mm}:${ss < 10 ? "0" + ss : ss}`;
}

const CheckLogin = () => {
  const { token } = Vue.prototype.$store.getters
  if (!token) {
    Vue.prototype.$navigateTo('/pages/login/index')
    throw new Error("您还没有登录");
  }
}

export default {
  install(Vue) {
    Object.assign(Vue.prototype, {
      $timeStr: TimeStr,
      $checkLogin: CheckLogin,
    });
  },
};
